local chuanwu = fk.CreateSkill{
    name = "jinld__chuanwu",
}

Fk:loadTranslationTable{
    ["jinld__chuanwu"] = "穿屋",
    [":jinld__chuanwu"] = "当你造成或受到属性伤害后，你可以弃置一名角色场上的一张武器牌并摸X张牌 (X为其攻击范围)。"..
    "每回合结束时，若本回合有至少两张武器牌以此法被弃置，你可以<a href='heg_transform_general'>变更副将</a>。",

    ["#jinld__chuanwu_choose"] = "穿屋：选择一名角色，弃置其场上的一张武器牌并摸其攻击范围张牌",
    ["@jinld__chuanwu-turn"] = "穿屋",
    ["@@ld__chuanwu_transform"] = "穿屋 已变更",

    ["$jinld__chuanwu1"] = "斩蛇穿屋，其志绥远。",
    ["$jinld__chuanwu2"] = "祝融侵库，剑怀远志。",
}

local H = require "packages/ol_hegemony/util"

local chuanwu_spec = {
    anim_type = "special",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(chuanwu.name) and data.damageType ~= fk.NormalDamage
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (p) return #p:getEquipments(Card.SubtypeWeapon) ~= 0 end)
        if #targets == 0 then return end
        local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            skill_name = chuanwu.name,
            prompt = "#jinld__chuanwu_choose",
            cancelable = true,
        })
        if #to > 0 then
        event:setCostData(self, { tos = to })
        return true
      end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local x = math.max(0, to:getAttackRange())
        room:throwCard(to:getEquipments(Card.SubtypeWeapon), chuanwu.name, to, player)
        if to.dead or player.dead then return end
        if x > 0 then
          player:drawCards(x, chuanwu.name)
        end
        room:addPlayerMark(player, "@jinld__chuanwu-turn", 1)
    end,
}

chuanwu:addEffect(fk.Damage,chuanwu_spec)
chuanwu:addEffect(fk.Damaged,chuanwu_spec)

chuanwu:addEffect(fk.TurnEnd,{
    anim_type = "special",
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(chuanwu.name) and player:getMark("@jinld__chuanwu-turn") > 1 and player:getMark("@@ld__chuanwu_transform") == 0
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
         if player:getMark("@@ld__chuanwu_transform") == 0
         and room:askToChoice(player, {choices = {"transform_deputy", "Cancel"}, skill_name = chuanwu.name}) ~= "Cancel" then
          room:setPlayerMark(player, "@@ld__chuanwu_transform", 1)
          H.transformGeneral(player, false, 3, false)
        end
    end,
})


return chuanwu